package com.bdth.mixapi.api.controller.index;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdth.mixapi.api.entity.Device;
import com.bdth.mixapi.api.entity.Typesum;
import com.bdth.mixapi.api.service.DeviceService;
import com.bdth.mixapi.api.service.TypesumService;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.CommonResult;
import com.bdth.mixapi.api.vo.TypesumVo;
import com.bdth.mixapi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-11
 */
@Api("设备类型总数表")
@RestController
@RequestMapping("/index")
public class TypesumController {

    @Autowired
    TypesumService typesumService;
    @Autowired
    DeviceService deviceService;

    @ApiOperation(value="获取最近更新的设备类型总数")
    @RequestMapping(value = "typesum/getResult", method = RequestMethod.GET)
    public CommonList<TypesumVo> getResult(){
        //查询有数据的最后一天
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("uptime");
        wrapper.last("limit 1");
        List<Device> list = deviceService.list(wrapper);
        List<TypesumVo> result = new ArrayList<>();
        String now = null;
        if(list!=null && list.size()>0) {
            now = DateUtil.format(list.get(0).getUptime());
            QueryWrapper<Device> t1query = new QueryWrapper<>();
            t1query.eq("type","1");
            t1query.eq("uptime",now);
            List<Device> lst1 = deviceService.list(t1query);
            TypesumVo typesumVo1 = new TypesumVo();
            typesumVo1.setType(1);
            typesumVo1.setAllsum(lst1.size());
            typesumVo1.setTypename("RDSS用户");
            result.add(typesumVo1);

            QueryWrapper<Device> t2query = new QueryWrapper<>();
            t2query.eq("type","2");
            t2query.eq("uptime",now);
            List<Device> lst2 = deviceService.list(t2query);
            TypesumVo typesumVo2 = new TypesumVo();
            typesumVo2.setType(2);
            typesumVo2.setAllsum(lst2.size());
            typesumVo2.setTypename("互联网用户");
            result.add(typesumVo2);

            QueryWrapper<Device> t3query = new QueryWrapper<>();
            t3query.eq("type","3");
            t3query.eq("uptime",now);
            List<Device> lst3 = deviceService.list(t3query);
            TypesumVo typesumVo3 = new TypesumVo();
            typesumVo3.setType(3);
            typesumVo3.setAllsum(lst3.size());
            typesumVo3.setTypename("物联网用户");
            result.add(typesumVo3);
            return CommonList.success(result);
        }else {
            return CommonList.fail("没有数据");
        }
    }
}

